package com.totoro.test.j8_time;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters.*;

import static java.time.temporal.TemporalAdjusters.*;

public class DateExample {
    private static void useLocalDate(){
        LocalDate date = LocalDate.of(2017,11,8) ;
        System.out.println(date.getYear());
        System.out.println(date.getMonth());
        System.out.println(date.getDayOfMonth());
        System.out.println(date.getDayOfWeek());
        System.out.println(date.getDayOfYear());

        int  y = date.get(ChronoField.YEAR) ;
        System.out.println(y);
        int m = date.get(ChronoField.MONTH_OF_YEAR) ;
        System.out.println(m);
        int day = date.get(ChronoField.DAY_OF_MONTH) ;
        System.out.println(day);
    }

    public static void duration(){
        Duration dt = Duration.between(LocalTime.of(13,45,10), LocalTime.of(13, 45, 20)) ;
        System.out.println(dt.getSeconds());

        Period dd = Period.between(LocalDate.of(2017,4,18),LocalDate.of(2017,11,8)) ;
        System.out.println(dd.getDays());
        System.out.println(dd.getMonths());
        System.out.println(dd.getUnits());
        System.out.println(dd.getChronology());
        System.out.println(dd.getYears());

    }

    public static void duration2(){
        Duration duration = Duration.ofSeconds(3) ;
        System.out.println(duration.getSeconds());

        Period period = Period.of(4,2,13) ;
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
        System.out.println(period.toString());
    }

    public static void temporalAdjuster(){
        LocalDate date = LocalDate.now() ;
        LocalDate dd = LocalDate.of(2017,10,1) ;
        System.out.println(date.with(dayOfWeekInMonth(4, DayOfWeek.MONDAY)));

        System.out.println(dd.with(dayOfWeekInMonth(1,DayOfWeek.TUESDAY)));

        System.out.println(date.with(firstDayOfMonth()));
        System.out.println(dd.with(firstDayOfMonth()));

        System.out.println(date.with(lastDayOfMonth()));
        System.out.println(dd.with(lastDayOfMonth()));

        System.out.println(date.with(lastDayOfYear()));
        System.out.println(dd.with(lastDayOfYear()));

        System.out.println(date.with(lastInMonth(DayOfWeek.MONDAY)));
        System.out.println(dd.with(lastInMonth(DayOfWeek.SUNDAY)));

        System.out.println(date.with(next(DayOfWeek.MONDAY)));
        System.out.println(dd.with(previous(DayOfWeek.MONDAY)));

        System.out.println(date.with(nextOrSame(DayOfWeek.MONDAY)));
        System.out.println(dd.with(previousOrSame(DayOfWeek.MONDAY)));
    }

    //返回下一个工作日的Adjuster
    static class NextWorkingDay implements TemporalAdjuster{

        @Override
        public Temporal adjustInto(Temporal temporal) {
            DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK)) ;
            int dayToAdd = 1 ;
            if(dow == DayOfWeek.FRIDAY)
                dayToAdd = 3 ;
            else if (dow == DayOfWeek.SATURDAY)
                dayToAdd = 2 ;

            return temporal.plus(dayToAdd, ChronoUnit.DAYS);
        }
    }


    public static void main(String[] args) {
        //useLocalDate();
        //duration() ;
        //duration2() ;
        temporalAdjuster() ;
    }
}
